<template>
  <div class="main">
    <div class="mid">
      <div class="mid-left">
        <div class="mid-left-img"></div>
      </div>
      <div class="mid-right">
        <div class="mid-right-log">
          <p>图书管理系统</p>
          <div class="mid-right-el">
            <el-form
              :model="ruleform"
              :rules="rules"
              ref="ruleform"
              label-width="200px"
            >
              <el-form-item prop="name">
                <el-input
                  type="text"
                  prefix-icon="el-icon-user"
                  v-model="ruleform.name"
                  placeholder="请输入账号"
                ></el-input>
              </el-form-item>

              <el-form-item prop="pass">
                <el-input
                  @keyup.enter.native="submitForm('ruleform')"
                  prefix-icon="el-icon-lock"
                  type="password"
                  v-model="ruleform.pass"
                  placeholder="请输入密码"
                ></el-input>
              </el-form-item>

              <el-form-item>
                <el-button type="button" @click="submitForm('ruleform')"
                  >登录</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/login/login";
export default {
  name: "logIn",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入账号"));
      } else if (value.length < 4 || value.length > 12) {
        callback("账号至少为4位数");
      } else {
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (value.length < 5 || value.length > 18) {
        callback("密码至少为五位数");
      } else {
        callback();
      }
    };
    return {
      ruleform: {
        name: "",
        pass: "",
      },
      rules: {
        name: [{ validator: validatePass, trigger: "blur" }],
        pass: [{ validator: validatePass2, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(ruleform) {
      this.$refs[ruleform].validate((valid) => {
        if (valid) {
          login({
            username: this.ruleform.name,
            password: this.ruleform.pass,
          }).then((res) => {
            if (res.code == 0) {
              sessionStorage.setItem("token", res.data.token);
              this.$router.push("/homepage/index");
            } else {
              this.$message({
                message: res.msg,
                type: "error",
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.ico-top {
  font-size: 20px;
  position: absolute;
  top: 226px;
  left: 85px;
}
.ico-bom {
  font-size: 20px;
  position: absolute;
  top: 269px;
  left: 85px;
}
.main {
  min-width: 1300px;
  min-height: 600px;
  width: 100vw;
  height: 100vh;
  background-image: url(@/assets/loginBg.png);
  background-repeat: no-repeat;
  background-size: 100% 100%;
}
.mid {
  border-radius: 10px;
  background-size: 100%;
  background-repeat: no-repeat;
  background-image: url(@/assets/roundBg.png);
  width: 900px;
  height: 500px;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  position: absolute;
  margin: auto;
}
.mid-left {
  width: 440px;
  height: 500px;
  float: left;
  border-radius: 0px;
}
.mid-left-img {
  width: 100%;
  height: 100%;
  background-position-y: 80px;
  background-position-x: 10px;
  background-repeat: no-repeat;
  background-size: 100%;
  background-image: url(@/assets/leftImg.png);
}
.mid-right {
  width: 440px;
  height: 500px;
  float: right;
  text-align: center;
}
.mid-right-log {
  width: 440px;
  height: 500px;
  text-align: center;
  position: relative;
  right: 30px;
}
.mid-right-el {
  margin-top: 170px;
  position: relative;
  right: 100px;
}
.mid-right-log p {
  text-align: center;
  font-family: "microsoft yahei";
  font-weight: 550;
  position: relative;
  top: 150px;

  font-size: 30px;
}
.input:hover {
  box-shadow: 0px 0px 20px -17px;
}
.input:active {
  transform: scale(0.95);
}
.input:focus {
  border: 2px solid grey;
}
.mid-right-log button {
  background-color: rgb(88, 197, 157);
  width: 240px;
  height: 40px;
  line-height: 10px;
  color: #ffffff;
  font-size: 16px;
  border: 1px solid #e8e8e8;
  transition: all 0.3s;
}
button:hover:not(:disabled) {
  background: rgba(35, 217, 108, 0.514);
  color: white;
}
button:active {
  color: #666;
  box-shadow: inset 4px 4px 12px #c5c5c5, inset -4px -4px 12px #ffffff;
}
</style>